package com.pm25.response;

import java.util.regex.Matcher;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.pm25.common.Constants;

public abstract class AbstractResponse {
	protected static Gson gson = new GsonBuilder().setDateFormat(Constants.GSON_DATE_PATTERN).create();
	
	protected final String responseBody;
	
	private String error ;
	private boolean isError = false;

	public AbstractResponse(String responseBody) {
		this.responseBody = responseBody;
		parseResponse();
	}
	
	public String getResponseBody() {
		return responseBody;
	}
	
	public boolean isError(){
		return isError;
	}
	
	private void parseResponse(){
		extractErrorMessage();
		if(isError==false){
			parseResult();
		}
	}
	
	public abstract void parseResult();
	
	private void extractErrorMessage(){
		Matcher matcher = Constants.RESPONSE_ERROR_PATTERN.matcher(responseBody);
		if(matcher.find()){
			isError = true ;
			error = matcher.group(Constants.RESPONSE_ERROR_PATTERN_GROUP);
		}
	}
	
	public String getError() {
		return error;
	}

}
